package com.yuanfengerp.basicInfoManage.warehouseinfo.service.impl;

import com.yuanfengerp.basicInfoManage.warehouseinfo.dao.IwarehouseinfoDao;
import com.yuanfengerp.basicInfoManage.warehouseinfo.model.Warehouseinfo;
import com.yuanfengerp.basicInfoManage.warehouseinfo.service.IWarehouseinfo;
import com.yuanfengerp.publicUtil.Result;
import com.yuanfengerp.publicUtil.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.sql.Timestamp;

@Service
public class WarehouseinfoImpl implements IWarehouseinfo {
    @Autowired
    IwarehouseinfoDao ihid;
    @Override
    public Result<List<Warehouseinfo>> selectWarehouseAll(Map<String,Object> params) {
        params.put("pagenum",((int) params.get("pagenum")-1) * (int) params.get("num"));
        List<Warehouseinfo> warehouseinfos = ihid.selectAll(params);
        if (!(warehouseinfos.isEmpty()))
            return Result.success(warehouseinfos, ihid.Count());
        return Result.failure(400,"没有这条信息");
    }

    @Override
    public Result<String> addwarehouse(Warehouseinfo warehouseinfo) {
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0);
        warehouseinfo.setWarehouse_ID(idWorker.nextId());
        warehouseinfo.setWarehouse_amendtime(new Timestamp(System.currentTimeMillis()));
        warehouseinfo.setWarehouse_time(new Timestamp(System.currentTimeMillis()));
        Integer addwarehouse = ihid.addwarehouse(warehouseinfo);
        if (addwarehouse > 0) {
            return Result.success("新增成功");
        }
        return Result.failure(400,"新增失败");
    }

    @Override
    public Result<String> updatewarehouse(Warehouseinfo warehouseinfo) {
        warehouseinfo.setWarehouse_amendtime(new Timestamp(System.currentTimeMillis()));
        Integer uwe = ihid.updatewarehouse(warehouseinfo);
        if (uwe > 0){
            return Result.success("修改成功");
        }
        return Result.failure(400,"修改失败");
    }

    @Override
    public Result<Warehouseinfo> selectwsinfo(String id) {
        Warehouseinfo selectwsinfo = ihid.selectwsinfo(id);
        if (selectwsinfo!=null){
            return Result.success(selectwsinfo);
        }
        return Result.failure(400,"没找到");
    }

    @Override
    public Result<String> deletewarehouse(String[] warehouse_ID) {
        //前端传回来一个id数组,根据id进行删除
        int d = 0;
        for (String id: warehouse_ID) {
            Integer deletewarehouse = ihid.deletewarehouse(id);
            d++;
        }
        if (warehouse_ID.length == d)
            return Result.success("删除成功");
        return Result.failure(400,"删除失败");
    }



}
